package com.smartstate.dataManagement;

import com.smartstate.dataManagement.mqtt.client.MyMQTTClient;
import com.smartstate.dataManagement.mqtt.common.GlobalMqttParam;
import com.smartstate.dataManagement.util.SpringUtils;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.oas.annotations.EnableOpenApi;

@SpringBootApplication
@EnableOpenApi
@MapperScan("com.smartstate.dataManagement.mapper")
@EnableTransactionManagement
@EnableScheduling
@EnableWebMvc
@EnableFeignClients
public class DataManagementApplication extends SpringBootServletInitializer{

  public static void main(String[] args) {
    SpringApplication.run(DataManagementApplication.class, args);
    GlobalMqttParam globalMqttParam = SpringUtils.getBean(GlobalMqttParam.class);
    String broker = globalMqttParam.getBroker();
    String userName = globalMqttParam.getUserName();
    String passWord = globalMqttParam.getPassWord();
    String clientId1 = globalMqttParam.getSubClientId1();
    String subTopic1 = globalMqttParam.getSubTopic1();
    int keepalive = globalMqttParam.getKeepalive();
    int timeout = globalMqttParam.getTimeout();
    MyMQTTClient myMQTTClient = new MyMQTTClient(broker, userName, passWord, clientId1, keepalive, timeout);
    try {
      myMQTTClient.connect();
      myMQTTClient.subscribe(subTopic1 + "/#", 1);
    } catch (MqttException e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(DataManagementApplication.class);
  }
}

